package net.airtheva.uniqueTask4;

import java.util.ArrayList;

import net.airtheva.uniqueTask4.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;

public class ControlWidget extends AppWidgetProvider {

	public static final String TAG = "ControlWidget";
	
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_TITLE = "title";
	public static final String KEY_STATUS = "status";
	
	public static final int REQUEST_CODE_ENTRY_ACTIVITY = 0;
	public static final int REQUEST_CODE_CONTROL = 1;
	
	private static ArrayList<Integer> AppWidgetIds;
	
	static {
		AppWidgetIds = new ArrayList<Integer>();
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		//super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		Log.d(TAG, "onUpdate.");
		
		for(int i = 0; i < appWidgetIds.length; i++) {
			
			int appWidgetId = appWidgetIds[i];
			AppWidgetIds.add(appWidgetId);
			
		}
		
		update(context, null);
		
	}

	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		//super.onEnabled(context);
	}

	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		//super.onDisabled(context);
	}

	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		//super.onDeleted(context, appWidgetIds);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		
		String action = intent.getAction();
		
		if(action.equals(PlayerService.BROADCAST_ACTION_APPWIDGET_UPDATE)) {
			update(context, intent);
		}
		else {
			super.onReceive(context, intent);
		}
		
	}

	protected void update(Context context, Intent intent) {
		
		//Log.d(TAG, "Update.");
		
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		//remoteViews.setOnClickPendingIntent(PendingIntent.getActivity(context, REQUEST_CODE_ENTRY_ACTIVITY, new Intent(context, EntryActivity.class), PendingIntent.FLAG_CANCEL_CURRENT));
		remoteViews.setOnClickPendingIntent(R.id.widget_previous, PendingIntent.getBroadcast(context, REQUEST_CODE_CONTROL, new Intent(PlayerService.BROADCAST_PREVIOUS), PendingIntent.FLAG_CANCEL_CURRENT));
		remoteViews.setOnClickPendingIntent(R.id.widget_toggle, PendingIntent.getBroadcast(context, REQUEST_CODE_CONTROL, new Intent(PlayerService.BROADCAST_TOGGLE), PendingIntent.FLAG_CANCEL_CURRENT));
		remoteViews.setOnClickPendingIntent(R.id.widget_next, PendingIntent.getBroadcast(context, REQUEST_CODE_CONTROL, new Intent(PlayerService.BROADCAST_NEXT), PendingIntent.FLAG_CANCEL_CURRENT));
		
		if(intent != null) {
			
			String artist = intent.getStringExtra(KEY_ARTIST);
			String title = intent.getStringExtra(KEY_TITLE);
			int status = intent.getIntExtra(KEY_STATUS, PlayerService.STATUS_UNKNOWN);
			
			remoteViews.setTextViewText(R.id.widget_artist, artist);
			remoteViews.setTextViewText(R.id.widget_title, title);
			
			switch(status) {
			case PlayerService.STATUS_PLAYING:
				remoteViews.setImageViewResource(R.id.widget_toggle, android.R.drawable.ic_media_pause);
				break;
			case PlayerService.STATUS_PAUSING:
				remoteViews.setImageViewResource(R.id.widget_toggle, android.R.drawable.ic_media_play);
				break;
			}
			

			
		}
		
		int size = AppWidgetIds.size();
		for(int i = 0; i < size; i++) {
			
			int appWidgetId = AppWidgetIds.get(i);
			appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
			
		}
	
	}
	
}
